package cn.cqie.attendance.manager.service.impl;

import cn.cqie.attendance.common.util.Page;
import cn.cqie.attendance.common.util.PageBean;
import cn.cqie.attendance.manager.dao.AdminClassesDao;
import cn.cqie.attendance.manager.pojo.AdminClasses;
import cn.cqie.attendance.manager.pojo.AdminClassesAdd;
import cn.cqie.attendance.manager.pojo.AdminClassesSerch;
import cn.cqie.attendance.manager.service.AdminClassesService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 697
 * @date 2021/6/18 17:15
 */
@Service
public class AdminClassesServiceImpl implements AdminClassesService {

    @Autowired
    private AdminClassesDao classesDao;


    /**
     * 查询所有班级信息 可模糊查询/组合查询
     * @param adminClassesSerch
     * @param pageSize    每一页的数量
     * @param pageNum 当前页
     * @return
     */
    @Override
    public PageBean<AdminClasses> queryAllClassesInfo(AdminClassesSerch adminClassesSerch, Integer pageSize, Integer pageNum) {

        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        PageHelper.startPage(pageNum,pageSize);

        List<AdminClasses> list = classesDao.queryAllClassesInfo(adminClassesSerch);
        int total = list.size();

        if (null == list || total == 0) {
            return new PageBean<>(201,"error",list);
        }
        return new PageBean<>(200,"success",list);
    }

    /**
     * 新增班级信息
     * @param adminClassesAdd
     * @return
     */
    @Override
    public Page addClasses(AdminClassesAdd adminClassesAdd) {
        adminClassesAdd.setSubmitTime(new Date());
        //判断是否重复
        int i = classesDao.queryCountClassesInfoByClassName(adminClassesAdd.getClassName());
        if (i > 0) {
            return Page.INFOERROR;
        }
        //继续执行新增
        else {
            int msg = classesDao.addClasses(adminClassesAdd);
            if (msg>0){
                return Page.SUCCESS;
            }else{
                return Page.FAIL;
            }
        }
    }




//    /**
//     * 根据班级名查询班级数量信息
//     * @param className 班级名
//     * @return
//     */
//    @Override
//    public int queryCountClassesInfoByClassName(String className) {
//        int i = classesDao.queryCountClassesInfoByClassName(className);
//        return i;
//    }


    /**
     * 根据ID查询班级信息
     * @param id id
     * @return
     */
    @Override
    public Page<AdminClasses> queryClassesInfoById(Integer id) {
        List<AdminClasses> list = classesDao.queryClassesInfoById(id);
        int total = list.size();
        if (null == list ||  total== 0) {
            return Page.INFOERROR;
        }
        return new Page<>(200, "Success",0,0, total, list);
    }


    /**
     * 根据ID修改班级信息
     * @param adminClassesAdd
     * @return
     */
    @Override
    public Page updateClassesInfoById(AdminClassesAdd adminClassesAdd) {
        adminClassesAdd.setSubmitTime(new Date());
        //判断是否重复
        int i = classesDao.queryClassCount(adminClassesAdd);
        if (i > 0) {
            return Page.INFOERROR;
        }
        //继续执行修改
        else {
            int msg = classesDao.updateClassesInfoById(adminClassesAdd);
            if (msg>0){
                return Page.SUCCESS;
            }else{
                return Page.FAIL;
            }
        }
    }


}
